package down3.biz;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import up7.database.DBConfig;
import up7.sql.SqlExec;
import up7.sql.SqlParam;
import up7.utils.PathTool;

public class FolderBuilder {

    /// <summary>
    /// 根据路径查找数据，而不是根据pidRoot
    /// </summary>
    /// <param name="pageIndex">页索引，基于1</param>
    /// <param name="id">文件夹ID</param>
    /// <returns></returns>
    public String build(Integer pageIndex,String id)
    {
        String pathRoot = this.getPathRel(id)+"/";// /test/child/
        Integer pageSize = 100;//每页100条数据
        Integer index = pageIndex;
        index = Math.max(index, 1);//基于1
        String where = String.format("CHARINDEX('%s',f_pathRel)>0 and f_fdTask=0",pathRoot);
        DBConfig dc = new DBConfig();
        if(dc.m_db.equals("mysql"))
        {
        	where = String.format("locate('%s',f_pathRel)>0 and f_fdTask=0", pathRoot);
        }
        else if(dc.m_db.equals("oracle"))
        {
        	where = String.format("instr(f_pathRel,'%s')>0 and f_fdTask=0", pathRoot);
        }
        
        SqlExec se = new SqlExec();
        JSONArray files = se.page("up7_files", "f_id", "f_id,f_nameLoc,f_pathSvr,f_lenLoc,f_sizeLoc,f_pathRel", pageSize, index, where, "f_time asc");
        
    	for(int i = 0 ; i < files.size() ; i++)
    	{
    		JSONObject f = (JSONObject)files.get(i);
    		String pathRel = f.getString("f_pathRel");
    		pathRel = pathRel.substring(pathRoot.length());
    		f.put("id", PathTool.guid());
    		f.put("f_id", f.getString("f_id"));
    		f.put("nameLoc", f.getString("f_nameLoc"));
    		f.put("pathSvr", f.getString("f_pathSvr"));
    		f.put("pathRel", pathRel);
    		f.put("lenSvr", f.getString("f_lenLoc"));    		
    	}

        if (files.size() > 0) return files.toString();
        return "";
    }

    /// <summary>
    /// 统计文件夹数量
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public Integer count(String id)
    {
        SqlExec se = new SqlExec();
        String pathRel = this.getPathRel(id);
        //子目录：/test/child/
        //根目录：/test/
        pathRel += "/";
        String where = String.format("CHARINDEX('%s',f_pathRel)>0 and f_fdTask=0",pathRel);
        Integer c = se.count("up7_files", where);
        return c;
    }

    /// <summary>
    /// 获取目录总大小
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public long totalSize(String id)
    {
        SqlExec se = new SqlExec();
        String pathRel = this.getPathRel(id);
        //子目录：/test/child/
        //根目录：/test/
        pathRel += "/";
        String where = String.format("CHARINDEX('%s',f_pathRel)>0 and f_fdTask=0", pathRel);
        String sql = String.format("select sum(f_lenLoc) from up7_files where %s", where);
        long c = se.execLong(sql);        
        return c;
    }

    /// <summary>
    /// 获取文件夹的相对路径：
    /// 子目录：/test/文件夹
    /// 根目录：/test
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    public String getPathRel(String id)
    {
        SqlExec se = new SqlExec();
        JSONObject fd = se.read("up7_files", "f_nameLoc,f_pathRel", new SqlParam[] { new SqlParam("f_id", id) });
        if (fd == null)
        {
            fd = se.read("up7_folders", "f_nameLoc,f_pathRel", new SqlParam[] { new SqlParam("f_id", id) });
        }
        String pathRel = fd.getString("f_pathRel");
        return pathRel;
    }
}
